// pages/updatephonenumber/updatephonenumber.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:'',
    wenzi:'获取验证码',
    code:'',
    getcode: app.data.url +'sheyingjia/myhuiyuan/getcode',
    changephone: app.data.url + 'sheyingjia/Xmyuser1/changephone',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var phone = options.phone;
    this.setData({phone:phone});
  },
  //修改号码
  changeinput:function(e){
    console.log(e);
    var str = e.detail.value;
  console.log(str);
  this.setData({phone:str});
  },
  changeinputs:function(e){
    var str = e.detail.value;
    console.log(str);
    this.setData({code:str});
  },
  //发送验证码
  huoquyanzm:function(){
    var that = this;
    var wenzi = this.data.wenzi;
    if (wenzi =='获取验证码'){
      var phone = that.data.phone;
      wx.showLoading({
        title: '加载中',
      })
      ea.request(that.data.getcode,{phone:phone},function(res){
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if(data.code==200){
          ea.TimeDown(60, function (number) {
            console.log(number);
            if (number == 0) {
              that.setData({ wenzi: '获取验证码' });
            } else {
              that.setData({ wenzi: number + '秒' });
            }
          })
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      })

    }
  },
  baocun:function(){
    var phone = this.data.phone;
    var code = this.data.code;
    var user = wx.getStorageSync('user');
    if(!code){
      wx.showToast({
        title: '请填入验证码',
        icon:'none'
      })
      return;
    }
    var user = wx.getStorageSync('user');
    var that = this;
    wx.showLoading({
      title: '保存中',
    })
    ea.request(that.data.changephone,{phone:phone,code:code,token:user.token},function(res){
      var data = res.data;
      console.log(data);
      if(data.code==200){
        wx.showToast({
          title: data.msg,
        })
        wx.setStorageSync('myphone', phone);
        setTimeout(function(){
          wx.navigateBack({
            delta:1
          })
        },app.data.times);
      }else{
        wx.showToast({
          title: data.msg,
          icon:'none'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})